package com.example.MatchGame;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.example.PullGame.Constant;
import com.example.PullGame.Menu;
import com.example.PullGame.PullDrawView;
import com.example.PullGame.PullGamePlay;
import com.example.PullGame.PullGameView;
import com.example.PullGame.PullState;
import com.example.wonderfulradish.R;

/**
 * Created by 陈勇劲 on 2017/10/4.
 */

public class MatchGameOver implements MatchState{
    private Resources res;
    private MatchMenu menu;
    private int score;
    private Bitmap bgBitmap, succeedBitmap,starBitmap,failBitmap;
    private Rect bgRect,againButtonRect,endButtonRect,boundr,startRect,succeedRect,overRect;
    private Bitmap[] againBitmaps,endBitmaps;
    private boolean[] isPress;
    private MatchDrawView matchActivity;
    public MatchGameOver(Resources res, int score, MatchMenu menu, MatchDrawView mainActivity) {
        this.matchActivity=mainActivity;
        this.res=res;
        this.score=score;
        this.menu=menu;
        load();
    }
    public  void load(){
        //加载背景图方块,按钮图方块,边框方块.
        bgBitmap= BitmapFactory.decodeResource(res, R.drawable.success_seg_png);
        bgRect=new Rect(menu.getBackgroundRect());
        //重玩按钮
        againBitmaps=new Bitmap[2];
        againBitmaps[0]=BitmapFactory.decodeResource(res, R.drawable.fail_btn2_png);
        againBitmaps[1]=BitmapFactory.decodeResource(res, R.drawable.icon_play);

        //结束按钮
        endBitmaps=new Bitmap[2];
        endBitmaps[0]=BitmapFactory.decodeResource(res, R.drawable.success_btn2_png);
        endBitmaps[1]=BitmapFactory.decodeResource(res, R.drawable.icon_play);

        //成功背景
        succeedBitmap=BitmapFactory.decodeResource(res,R.drawable.success_def_png);

        //绘制星星
        starBitmap=BitmapFactory.decodeResource(res,R.drawable.success_xing1_png);

        //
        failBitmap=BitmapFactory.decodeResource(res,R.drawable.fail_def_png);

        isPress=new boolean[2];

       // boundr=new Rect(
       //         (int) (bgRect.left+50*Menu.widthM),
       //         (int) (bgRect.top+40*Menu.heightM),
       //         (int) (bgRect.left+bgRect.width()*2/6),
       //         (int) (bgRect.top+bgRect.height()*3/4));

        boundr=new Rect(
                (int) (bgRect.left+bgRect.width()*2/6),
                (int) (bgRect.top+bgRect.height()*2/4),
                (int) (bgRect.left+bgRect.width()*4/6),
                (int) (bgRect.top+bgRect.height()*3/4));
        
        //重玩按钮坐标
        int left=boundr.left+boundr.width()/2-againBitmaps[0].getWidth();
        int top=boundr.top+boundr.height()/2;
        int right=boundr.left+boundr.width()/2;
        int bottom=boundr.top+boundr.height()/2+againBitmaps[0].getHeight();
        againButtonRect=new Rect(left, top, right, bottom);

        //结束按钮坐标
        left=boundr.left+boundr.width()/2;
        top=boundr.top+boundr.height()/2;
        right=boundr.left+boundr.width()/2+againBitmaps[0].getWidth();
        bottom=boundr.top+boundr.height()/2+againBitmaps[0].getHeight();
        endButtonRect=new Rect(left, top, right, bottom);

        left=(int) (Constant.SCREEN_WIDTH*1/9);
        top= (int) (bgRect.top+bgRect.height()*2/14);
        right=(int) (Constant.SCREEN_WIDTH*7/9);
        bottom=(int) (bgRect.top+bgRect.height()*7/12);
        overRect=new Rect(left, top, right, bottom);


    }


    @Override
    public void draw(Canvas canvas, Paint paint) {
        // TODO Auto-generated method stub
        //画背景方法
        drawBg(canvas, paint);
        //画游戏结束,已经最后得分方法
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextSize(100*Menu.heightM);
        canvas.drawText("游戏结束", boundr.left+boundr.width()/2-60*2*Menu.heightM,
                boundr.top+60*Menu.heightM, paint);
        paint.setTextSize(60*Menu.heightM);
        canvas.drawText("您的得分是："+score,
                boundr.left+boundr.width()/2-30*4*Menu.heightM,
                boundr.top+120*Menu.heightM,
                paint);
        //画按钮
        drawButton(canvas, paint);
        if(score==0){
            canvas.drawBitmap(failBitmap, null, overRect,paint);

        }else if (score<=3&&score>0) {
            canvas.drawBitmap(starBitmap,100,100,paint);
            canvas.drawBitmap(succeedBitmap,null,overRect,paint);
        }
        else if (score<=6&&score>=3) {
            canvas.drawBitmap(starBitmap,100,100,paint);
            canvas.drawBitmap(starBitmap,300,100,paint);
            canvas.drawBitmap(succeedBitmap,null,overRect,paint);
        }else if (score<=9&&score>=6) {
            canvas.drawBitmap(starBitmap,100,100,paint);
            canvas.drawBitmap(starBitmap,300,100,paint);
            canvas.drawBitmap(starBitmap,500,100,paint);
            canvas.drawBitmap(succeedBitmap,null,overRect,paint);
        }
        else if (score<=18&&score>=9) {
            canvas.drawBitmap(starBitmap,100,100,paint);
            canvas.drawBitmap(starBitmap,300,100,paint);
            canvas.drawBitmap(starBitmap,500,100,paint);
            canvas.drawBitmap(succeedBitmap,null,overRect,paint);
        }

    }

    //画游戏结束的背景
    private void drawBg(Canvas canvas, Paint paint) {
        //类似于game类里画时间得分的方法,绘制空心矩形
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        canvas.drawRect( Constant.SCREEN_WIDTH/2,  Constant.SCREEN_HEIGHT/2, Constant.SCREEN_WIDTH/2, Constant.SCREEN_HEIGHT/2, paint);
        canvas.drawBitmap(bgBitmap, null, bgRect, paint);
        paint.setAntiAlias(true);
        paint.setColor(0xFFffb400);
        paint.setStrokeWidth(3);

        canvas.drawRect(boundr, paint);
        paint.setColor(0xFF884702);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(boundr, paint);
        canvas.drawRect(bgRect, paint);
    }
    private void drawButton(Canvas canvas, Paint paint) {
        paint.setStyle(Paint.Style.FILL);
        // 根据是否按下条件画重来按钮
        if(!isPress[0]){
            canvas.drawBitmap(againBitmaps[0], null, againButtonRect, paint);
        }else{
            canvas.drawBitmap(againBitmaps[1], null, againButtonRect, paint);
        }
        // 根据是否按下条件画结束按钮
        if(!isPress[1]){
            canvas.drawBitmap(endBitmaps[0], null, endButtonRect, paint);
        }else{
            canvas.drawBitmap(endBitmaps[1], null, endButtonRect, paint);
        }
    }

    @Override
    public void touch(MotionEvent event) {
        int x=(int) event.getX();//获得触摸点X坐标
        int y=(int) event.getY();//获得触摸点y坐标
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(againButtonRect.contains(x, y)){
                    isPress[0]=true;
                }
                if(endButtonRect.contains(x, y)){
                    isPress[1]=true;
                }
                break;

            case MotionEvent.ACTION_UP:
                if(againButtonRect.contains(x, y)){
                    MatchGameView.matchState=new MatchGamePlay(res,menu,matchActivity);//重新游戏
                }
                if(endButtonRect.contains(x, y)){
                    System.exit(0);			//退出游戏
                }
                isPress[0]=false;
                isPress[1]=false;
                break;
        }
    }

}
